package com.quanquan.store.service;

import com.quanquan.store.entity.CouponHistory;
import com.quanquan.store.entity.History;
import com.quanquan.store.repository.CouponHistoryRepository;
import com.quanquan.store.repository.HistoryReposity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.DateUtils;
import utils.UUIDGenerator;

import java.util.Date;
import java.util.List;

/**
 * @ClassName:HistoryService
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/3/20 10:45
 * @Version 1.0
 */
@Service
public class HistoryService {

    private final HistoryReposity historyReposity;
    private final CouponHistoryRepository couponHistoryRepository;

    @Autowired
    public HistoryService(HistoryReposity historyReposity, CouponHistoryRepository couponHistoryRepository) {
        this.historyReposity = historyReposity;
        this.couponHistoryRepository = couponHistoryRepository;
    }

    /**
     * 保存商户搜索记录
     * @param userId
     * @param content
     * @return
     */
    public boolean saveHistory(String userId,String content) {
        Date date = new Date();
        History history = new History();
        history.setId(UUIDGenerator.getUUID());
        history.setCreateTime(DateUtils.dateToString(date, DateUtils.TIMESTAMP));
        history.setUpdateTime(DateUtils.dateToString(date, DateUtils.TIMESTAMP));
        history.setStatusShow(0);
        history.setStatusRemove(0);
        history.setCountent(content);
        history.setUserId(userId);
        historyReposity.saveAndFlush(history);
        return true;
    }

    /**
     * 保存商品搜索记录
     * @param userId
     * @param content
     * @return
     */
    public boolean saveCouponHistory(String userId,String content) {
        Date date = new Date();
        CouponHistory couponHistory = new CouponHistory();
        couponHistory.setId(UUIDGenerator.getUUID());
        couponHistory.setCreateTime(DateUtils.dateToString(date, DateUtils.TIMESTAMP));
        couponHistory.setUpdateTime(DateUtils.dateToString(date, DateUtils.TIMESTAMP));
        couponHistory.setStatusShow(0);
        couponHistory.setStatusRemove(0);
        couponHistory.setCountent(content);
        couponHistory.setUserId(userId);
        couponHistoryRepository.saveAndFlush(couponHistory);
        return true;
    }

    /**
     * 通过userId查找商户历史记录
     * @param userId
     * @param page
     * @param size
     * @return
     */
    public List<History> queryByUserId(String userId, Integer page, Integer size) {
        List<History> historyList = historyReposity.queryByUserId(userId,page*size,size);
        return historyList;
    }

    /**
     * 通过userId查找商品搜索历史记录
     * @param userId
     * @param page
     * @param size
     * @return
     */
    public List<CouponHistory> queryCouponByUserId(String userId, Integer page, Integer size) {
        List<CouponHistory> couponHistories = couponHistoryRepository.findByUserId(userId,page*size,size);
        return couponHistories;
    }

    /**
     * 清除记录
     * @param userId
     * @return
     */
    public boolean removeHistory(String userId) {
        List<History> historyList = historyReposity.queryByUserId(userId);
        for (History history : historyList){
            history.setStatusRemove(1);
            historyReposity.saveAndFlush(history);
        }
        return true;
    }

    /**
     * 清除商品搜索记录
     * @param userId
     * @return
     */
    public boolean removeCouponHistory(String userId) {
        List<CouponHistory> couponHistories = couponHistoryRepository.findByUserId(userId);
        for (CouponHistory couponHistory : couponHistories){
            couponHistory.setStatusRemove(1);
            couponHistoryRepository.saveAndFlush(couponHistory);
        }
        return true;
    }



}
